package com.byzk.p2p.admin.system.controller;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.system.dto.Menu;
import com.byzk.p2p.admin.system.service.MenuService;
import com.byzk.p2p.admin.system.vo.MenuVo;

@Controller
@RequestMapping("/admin/menu")
public class MenuController extends AdminBaseController {

	@Autowired
	private MenuService menuService;

	@RequestMapping("/list")
	public String listMenu(Model model, 
			@RequestParam(value = "index", required = false, defaultValue = "1") int index,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
		PageInfo<MenuVo> pageInfo = menuService.menuList(null,index, limit);
		model.addAttribute("pageInfo", pageInfo);
		String result = ftl(Thread.currentThread().getStackTrace());
		return result;
	}
	
	@RequestMapping(value="/saveMenu")
	public void saveMenu(@RequestBody Menu menu) {
		try {
			if (StringUtils.isBlank(menu.getName())) {
				printJSON(new JsonMsg(1, "菜单名称不能为空"));
				return;
			}
			if (StringUtils.isBlank(menu.getUrl())) {
				printJSON(new JsonMsg(2, "菜单访问URL"));	
				return;
			}
			if (menu.getOrder() == null) {
				printJSON(new JsonMsg(3, "菜单排序号不能为空"));
				return;
			}
			if (menu.getId() == null) {
				menu.setCreateTime(new Date());
			}
			Integer i = menuService.saveMenu(menu);
			if (i >0) {
				printJSON(new JsonMsg(-1, "操作成功"));
			}else{
				printJSON(new JsonMsg(0, "操作失败"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/settingMenu")
	public String settingMenu(Model model,@RequestParam(value="id",required = false ,defaultValue = "0") int id){
		MenuVo menuVo = menuService.getMenu(id);
		model.addAttribute("item", menuVo);
		model.addAttribute("itemList", menuService.getAllMenu());
		return ftl(Thread.currentThread().getStackTrace());
	}
	
}
